<template>
  <div class="login_conitar">
    <div class="login_box">
      <!--头像区域 -->
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="loginFormref"
        :model="formData"
        :rules="formrules"
        class="formItem"
      >
        <el-form-item prop="username">
          <el-input
            v-model="formData.username"
            prefix-icon="iconfont icon-touxiang"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            type="password"
            prefix-icon="iconfont icon-suo"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resClick">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {

  data() {
    return {
      formData: {
        username: "admi",
        password: 12345
      },
      // 验证规则
      formrules: {
        username: [
          { required: true, message: '请输入用户名那个', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
      }
    }
  },
  methods: {
    resClick() {
      this.$refs.loginFormref.resetFields()
    },
    login() {
      this.$refs.loginFormref.validate(async (valid) => {
        console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.post('/login', this.formData)
        console.log(res)
        if (res.meta.status !== 200) return this.$message({ showClose: true, message: '登录失败', type: 'error' })
        this.$message({ showClose: true, message: '登录成功', type: 'success' })
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    // 点击回车键登录
    keyDown(e) {
      // 回车则执行登录方法 enter键的ASCII是13

      if (e.keyCode === 13) {
        this.login(); // 定义的登录方法
      }
    },

  },
  mounted() {
    // 绑定监听事件
    window.addEventListener("keydown", this.keyDown)

  },
  destroyed() {
    // 销毁事件
    window.removeEventListener("keydown", this.keyDown, false)
  }
}
</script>

<style lang="less" scoped>
.login_conitar {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;

  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: #2b4b6b;
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 50%;
    padding: 10px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #fff;
    }
  }

  .formItem {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>